﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the current status of user by his ID.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_Get(long uid, ApiCallback<Activity> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));

            return this.SendRequest("activity.get", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the current status of current user.
        /// </summary>        
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_Get(ApiCallback<Activity> callback)
        {
            return this.SendRequest("activity.get", callback, null);
        }


        /// <summary>
        /// Sets the status of the current user.
        /// </summary>
        /// <param name="text">The text of activity.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_Set(string text, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("text", text));

            return this.SendRequest("activity.set", callback, list.ToArray());
        }


        /// <summary>
        /// Gets the status history by user ID.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_GetHistory(long uid, ApiCallback<ActivityHistory[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));

            return this.SendRequest("activity.getHistory", callback, list.ToArray());
        }


        /// <summary>
        /// Deletes the status history item.
        /// </summary>
        /// <param name="aid">The activity ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_DeleteHistoryItem(long aid, ApiCallback<bool> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("aid", aid.ToString()));

            return this.SendRequest("activity.deleteHistoryItem", callback, list.ToArray());
        }


        /// <summary>
        /// Gets update news of friends statuses of cutrrent user.
        /// </summary>
        /// <param name="timestamp">The timestamp.</param>
        /// <param name="offset">The offset, needed to get the sub-set.</param>
        /// <param name="count">The count of status items.</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Activity_GetNews(long timestamp, long offset, int count, ApiCallback<ActivityNews[]> callback)
        {
            List<Parameter> list = new List<Parameter>();

            list.Add(new Parameter("timestamp", timestamp.ToString()));
            list.Add(new Parameter("offset", offset.ToString()));
            list.Add(new Parameter("count", count.ToString()));

            CallbackJsonPreprocess_Handler h = delegate(string s)
            {
                if (s.StartsWith("{\"error"))
                    return s;

                int start = s.IndexOf("[") + 1;
                int len = s.IndexOf(",") - start + 1;

                return s.Remove(start, len);
            };

            return this.SendRequest(
                "activity.getNews",
                new CallbackData() { Callback = callback, CallbackJsonPreprocess = h },
                list.ToArray());
        }
    }
}
